package approve.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import resource.pojo.ListReference;

import nguyen.dao.ListDocuments;
import nguyen.dao.ReceiverDocumentDAO;

import model.pojo.Document;
import model.pojo.Employee;
import model.pojo.Re_ListApprove;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import approve.action.Re_ApproveForm_lessthan10.DialogButtonClickHandler;
import approve.action.Re_ApproveForm_lessthan10.DialogSelectionClickHandler;

public class Re_ApproveForm14 extends Activity{
	
	//component
	String database = "admin";	
	String lang = "EN";
	Bundle language=null;
	Employee emp=null;
	Document doc=null;
	//layout
	TextView la_title,la_content,la_createDate,la_employee,la_approver,date;
	
	EditText title,employee,comment;
	WebView content;	
	Button approve,reject,reference;
	//new load
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.re_approveform14);
		getComponent();		
		
		loadBundle();
		loadLanguage();
		
		viewDocument();	
		handleApprove();
		handleReject();	
		handleReference();
	}
	
	private void warningReject() {
		AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle(language.getString("reject"));
		 final TextView textView = new TextView(this);
		 textView.setText(language.getString("are-you-sure"));
		 textView.setPadding(20,0,0,0);
		 helpBuilder.setView(textView);
		 helpBuilder.setPositiveButton(language.getString("ok"), new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int which) {
		    	ReceiverDocumentDAO.rejectDocument(emp.getIdemployee(), database, 
						doc.getiDGroupApprove(), emp.getImage(), comment.getText().toString());
				//reload list not approve
				Intent intent=new Intent(Re_ApproveForm14.this,Re_NotApprovedDocument.class);
				getIntent().removeExtra("doc");
				intent.putExtras(getIntent().getExtras());
				
				startActivity(intent);
		    }
		   });

		 helpBuilder.setNegativeButton(language.getString("cancel"), new DialogInterface.OnClickListener() {
		
		  public void onClick(DialogInterface dialog, int which) {
		   // Do nothing
		  }
		 });
		 // Remember, create doesn't show the dialog
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();

	}
	
	private void warningApprove() {
		AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle(language.getString("Approve"));
		 final TextView textView = new TextView(this);
		 textView.setText(language.getString("are-you-sure"));
		 textView.setPadding(20,0,0,0);
		 helpBuilder.setView(textView);
		 helpBuilder.setPositiveButton(language.getString("ok"), new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int which) {
		    	ReceiverDocumentDAO.acceptDocument(emp.getIdemployee(), emp.getImage(), database, doc.getiDGroupApprove(),
						comment.getText().toString());
				//reload list not approve
				Intent intent=new Intent(Re_ApproveForm14.this,Re_NotApprovedDocument.class);
				getIntent().removeExtra("doc");
				intent.putExtras(getIntent().getExtras());				
				startActivity(intent);
		    }
		   });

		 helpBuilder.setNegativeButton(language.getString("cancel"), new DialogInterface.OnClickListener() {
		
		  public void onClick(DialogInterface dialog, int which) {
		   // Do nothing
		  }
		 });
		 // Remember, create doesn't show the dialog
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();

	}
	
	private void loadLanguage() {
		if(language!=null){
			la_title.setText(language.getString("title"));
			la_content.setText(language.getString("content"));			
			la_createDate.setText(language.getString("created-date")+": ");
			
			la_employee.setText(language.getString("writer")+"/"+language.getString("Department"));
			la_approver.setText(language.getString("approver"));
			
			approve.setText(language.getString("Approve"));
			reject.setText(language.getString("reject"));
			reference.setText(language.getString("reference"));
			comment.setHint(language.getString("comment"));
			
		}
		
	}



	private void loadData(List<Re_ListApprove> listApprover) {
		
//		String[] arr=ReceiverDocumentDAO.getDepartment_EmployeeName(doc.getiDEmployee(), database, lang);
//		if(arr!=null && arr.length==2){
//			departmentL=arr[0];
//			employeeNameL=arr[1];
//		}
				
		if(listApprover!=null){
			LinearLayout re_ListApprover=(LinearLayout) findViewById(R.id.re_listapprover);
			int length=listApprover.size();
			for(int i=0;i<length;i++){
				TextView app_name=new TextView(this);
				app_name.setText(listApprover.get(i).getEmployeeName()+"("+listApprover.get(i).getDepartment()+")");
				re_ListApprover.addView(app_name);
				
//				TextView app_department=new TextView(this);
//				app_department.setText(listApprover.get(i).getDepartment());
//				re_ListApprover.addView(app_department);
				
				TextView app_status_date=new TextView(this);
				app_status_date.setPadding(6,0,0,0);
				Date dateTemp=listApprover.get(i).getDateApproved();
				app_status_date.setText(" _"+listApprover.get(i).getStatus()+ ((dateTemp!=null && !"null".equals(dateTemp)) ? " _ "+dateTemp : ""));
				re_ListApprover.addView(app_status_date);
				
//				TextView app_viewcomment=new TextView(this);
//				app_viewcomment.setText("Comment link");
//				re_ListApprover.addView(app_viewcomment);
				if(i<length-1){
					TextView app_line=new TextView(this);
					app_line.setHeight(1);
					app_line.setBackgroundColor(255255255);
					re_ListApprover.addView(app_line);
				}
				
			}
			
		}
	}

	public void handleApprove() {
		approve.setOnClickListener(new View.OnClickListener() {		
			public void onClick(View v) {
				warningApprove();
			}
		});
	}
	
	private void handleReject() {
		reject.setOnClickListener(new View.OnClickListener() {	
			public void onClick(View v) {
				warningReject();
			}
		});
		
	}
	
	//reference
	private void handleReference() {
		reference.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				getAllEmpsName();
				showDialogReference();
			}
		});

	}

	String[] _options;
	protected boolean[] _selections;
	ArrayList<String> arr = new ArrayList<String>();
	ArrayList<String> emps = new ArrayList<String>();
	Integer count = 0;
	ArrayList<Employee> list = new ArrayList<Employee>();
	ListDocuments call = new ListDocuments();

	private void showDialogReference() {
		new AlertDialog.Builder(this)
				.setTitle(language.getString("reference"))
				.setMultiChoiceItems(loadOptions(), loadSelections(),
						new DialogSelectionClickHandler())
				.setPositiveButton(language.getString("ok"),
						new DialogButtonClickHandler())
				.setNegativeButton(language.getString("cancel"),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface arg0, int arg1) {

							}
						}).create().show();
	}

	public String[] loadOptions() {
		_options = new String[arr.size()];
		for (int i = 0; i < arr.size(); i++) {
			_options[i] = arr.get(i);
		}
		return _options;
	}

	public boolean[] loadSelections() {
		_selections = new boolean[_options.length];
		return _selections;
	}

	public class DialogSelectionClickHandler implements
			DialogInterface.OnMultiChoiceClickListener {
		public void onClick(DialogInterface dialog, int clicked,
				boolean selected) {
			if (selected == true) {
				emps.add(_options[clicked].toString());
				count++;
			} else {
				for (int i = 0; i < emps.size(); i++) {
					if (_options.toString().equals(emps.get(i))) {
						emps.remove(i);
						count--;
					}
				}
			}
		}
	}

	public class DialogButtonClickHandler implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int clicked) {
			switch (clicked) {
			case DialogInterface.BUTTON_POSITIVE:
				ReferenceDocument();

			}
		}
	}
	private void ReferenceDocument(){
		System.out.println("database:" + database);
		List<ListReference> selects = new ArrayList<ListReference>();
		int j = 0;
		for (int i = 0; i < emps.size(); i++) {
			for (Employee employee : list) {
				if (employee.getFullName().equals(emps.get(i).toString())) {
					//emps.set(i, emp.getIdemployee());
					selects.add(new ListReference(employee.getIdemployee(),emp.getIdemployee(),doc.getiDGroupApprove(),4));
					j++;
				}
			}
		}
		ReceiverDocumentDAO.referenceDoc(database, doc.getId(), selects);
	}
	
	public ArrayList<Employee> getList() {
		return list;
	}

	public void setList(ArrayList<Employee> list) {
		this.list = list;
	}

	public void getAllEmpsName() {
		list = call.getAllEmp(database);
		for (int i = 0; i < list.size(); i++) {
			arr.add(list.get(i).getFullName());
		}
	}
	//end reference

	private void loadBundle(){
		Bundle extras= getIntent().getExtras();
		if(extras!=null){
			database=extras.getString("database");
			lang=extras.getString("lang");
			emp=(Employee) extras.get("em");
			doc=(Document) extras.get("doc");	
			language=extras.getBundle(lang);
		}
	}
	
	private void getComponent(){
		la_title=(TextView) findViewById(R.id.la_title);
		la_content=(TextView) findViewById(R.id.la_content);
		la_createDate=(TextView) findViewById(R.id.la_createDate);
		la_employee=(TextView) findViewById(R.id.la_employee);
		la_approver=(TextView) findViewById(R.id.la_approver);
		
		
		title=(EditText) findViewById(R.id.re_approveform_title);
		employee=(EditText) findViewById(R.id.re_approveform_employee);
		content=(WebView) findViewById(R.id.re_approveform_content);
		comment=(EditText) findViewById(R.id.re_approveform_Comment);
		date=(TextView) findViewById(R.id.re_approveform_date);
		
		approve=(Button) findViewById(R.id.re_approve);
		reject=(Button) findViewById(R.id.re_reject);
		reference = (Button) findViewById(R.id.re_reference);
	}
	
	private void viewDocument(){
		if(doc!=null){
			List array=ReceiverDocumentDAO.getListApprover(database, doc.getId(),lang);
			loadData((List<Re_ListApprove>) array.get(0));
			
			title.setText(doc.getTitle());
			
			
			employee.setText(doc.getEmployeeName()+"/"+doc.getDepartment());
			
			
			date.setText(doc.getTimecreate().toString());
			
			final String mimeType = "text/html";
			String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
			content.loadData(header+doc.getContent(), mimeType, null);
			System.out.println(content.toString());
		}
	}
	
}
